package blackberry.util.field;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.decor.Background;

abstract public class DjPreparedLabelManager extends Manager {

	protected DjLabelField _itemLabel;
	protected DjLabelField _infoLabel;
	
	protected static final int DEFAULT_WIDTH = Display.getWidth();
	protected int _width;
	
	private Background _focusedBackground = DjFieldInfo.DEFAULT_FOCUSED_BACKGROUND;
	
	public DjPreparedLabelManager(DjLabelField itemLabel, DjLabelField infoLabel, int width) {
		super(0);
		_itemLabel = itemLabel;
		_infoLabel = infoLabel;
		add(itemLabel);
		add(infoLabel);
		setWidth(width);
	}
	
	protected void setWidth(int width) {
		_width = width;
		_itemLabel.setTrimLabel(getPreferredWidth());
		_infoLabel.setTrimLabel(getPreferredWidth());
	}
	
	/**
	 * 필드의 width 를 설정한다. 이 값에 맞춰서 줄바꿈이 이루어지고 height 의 값이 정해진다.
	 * 적절한 height를 반환하도록 getPreferredHeight() 를 재정의 하자.
	 */
	public int getPreferredWidth() {
		return _width;
	}
	
	abstract public int getPreferredHeight();
	
	/**
	 * background 설정 메서드
	 */
	public Background getFocusedBackground() {
		return _focusedBackground;
	}

	public void setFocusedBackground(Background _focusedBackground) {
		this._focusedBackground = _focusedBackground;
	}

	/**
	 * focus 상태가 되면 Background 를 변경한다.
	 */
	protected void onFocus(int direction) {
		super.onFocus(direction);
		setBackground(_focusedBackground);
	}
	
	protected void onUnfocus() {
		super.onUnfocus();
		setBackground(null);
	}

	/**
	 * touch 이벤트 메서드.
	 * down 이벤트때 setFocus() 를 호출해서 background 가 변경되도록 돕는다.
	 */
	protected boolean touchEvent(TouchEvent message) {
		if (message.getEvent() == TouchEvent.DOWN)
			setFocus();
		return super.touchEvent(message);
	}
}
